﻿@{
    ViewBag.Title = "Productos";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using Helena.Areas.Productos.Models
@using Telerik.Web.Mvc.UI
@model IEnumerable<ProductoView>  
@Html.Telerik().ScriptRegistrar().Globalization(true)

<script type="text/javascript" >

    function ventas_onRowDataBound(e) {
        var grid = $(this).data('tGrid');
        expandFirstRow(grid, e.row);
    }

    function expandFirstRow(grid, row) {
        if (grid.$rows().index(row) == 0) {
            grid.expandRow(row);
        }
    }

</script>

<div class="block big" style="position: relative">
    <!-- Block Begin -->
    <div class="titlebar">
        <h3>
            Administración de Productos
        </h3>
    </div>
    <div class="block_cont">
        @(Html.Telerik().Grid(Model)
        .Name("ProductosGrid")
        .Localizable("es-ES")
        .Filterable()
        .ToolBar(commands => commands.Insert())
                .Editable(editing => editing
                .Mode(GridEditMode.PopUp)
                                .TemplateName("TemplateProducto")
                                      )
                .DataKeys(keys => keys.Add(o => o.ID))
                    .Columns(columns =>
                    {
                        columns.ForeignKey(c => c.categoriaID, (System.Collections.IEnumerable)ViewData["Categorias"], "ID", "NombreCategoria").Width(100);
                        columns.ForeignKey(c => c.campanaID, (System.Collections.IEnumerable)ViewData["Campanas"], "ID", "NombreCampana").Width(100);
                        columns.Bound(c => c.Descripcion).Width(260);
                        columns.Bound(c => c.cantidad).Width(80);
                        columns.Bound(c => c.preciofabrica).Width(80).Format("{0:c}");
                        columns.Bound(c => c.precioventa).Width(80).Format("{0:S/ ###,###.##}");
                        columns.ForeignKey(c => c.unidadID, (System.Collections.IEnumerable)ViewData["Unidades"], "ID", "descripcion").Width(90);
                        columns.Bound(c => c.Estado).Width(80).ClientTemplate("<input type='checkbox' disabled='disabled' name='Estado' <#= Estado? checked='checked' : '' #> />");
                        columns.Command(commands =>
                        {
                            commands.Edit().ButtonType(GridButtonType.Image);
                            commands.Custom("AddSubProducto")
                                .Text("+")
                                .DataRouteValues(c => c.Add(x => x.ID).RouteKey("ProductoID"))
                                .Action("AddSubProduct", "Producto");
                        }).Title("Acciones").Width(100);
                    })
                    .ClientEvents(events => events.OnRowDataBound("ventas_onRowDataBound"))
                    .DetailView(details => details.ClientTemplate(
                            Html.Telerik().Grid<DetalleProductoView>()
                                .Name("Venta_<#= ID #>")
                                .DataKeys(keys => keys.Add(o => o.ID))
                                .Columns(columns =>
                                {
                                    columns.ForeignKey(o => o.ProductoSID, (System.Collections.IEnumerable)ViewData["Productos"], "ID", "descripcion").Width(101);
                                    columns.Bound(o => o.PrecioVenta).Width(140);
                                    columns.Bound(o => o.Cantidad ).Width(200);
                                    columns.ForeignKey(c => c.UnidadID, (System.Collections.IEnumerable)ViewData["Unidades"], "ID", "descripcion").Width(90);
                                })
                                .DataBinding(dataBinding => dataBinding.Ajax()
                                .Select("_DetalleProducto", "Producto", new { ProductoID = "<#= ID #>" }))
                                .Pageable()
                                .Sortable()
                                .Filterable()
                                .ToHtmlString()
                    ))
                    .Pageable(paging => paging
                                       .PageSize(20)
                                       .Style(GridPagerStyles.Status).PageOnScroll(true))
                    .Sortable()
                    .Scrollable(scrolling => scrolling.Height(250))
                    .PrefixUrlParameters(false)
                    .DataBinding(dataBinding => dataBinding
                    .Ajax()
                        .Select("Select", "Producto")
                        .Insert("Insert", "Producto")
                        .Delete("Delete", "Producto")
                        .Update("Update", "Producto")
                                )
                )

            </div>
    <!-- Table Wrapper End -->

            <table style="width:20%"> 
                <tr> 
                    <td style="background-color: #CCFF99; color: #808080; font-weight: bold; font-size: 20px; border-style: ridge">
                    <@Html.ActionLink("ExportarExcel", "ExportarProductos", "Producto", null)>
                    </td>
                </tr>
            </table> 

</div>
